﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：12/20/2024 3:44:19 PM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion

namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:LanguagePackage
    /// 描    述:
    /// 修改时间:12/20/2024 3:44:19 PM
    /// </summary>
    public class LanguagePackage
    {

        #region 成员变量

        #region private



        #endregion


        #region protected

        protected List<LanguageFile> m_Languages;

        #endregion


        #region public
        /// <summary>
        /// 默认语言文件名称
        /// </summary>
        public const string DefaultLanguageFileName = "default";

        /// <summary>
        /// 枚举语言文件名称
        /// </summary>
        public const string EnumLanguageFileName = "enums";

        /// <summary>
        /// 语言键值
        /// </summary>
        public enum emLanguageKey
        {
            /// <summary>
            /// 中文
            /// </summary>
            zh,
            /// <summary>
            /// 英文
            /// </summary>
            en,

            /// <summary>
            /// 日文
            /// </summary>
            jp,

            /// <summary>
            /// 韩语
            /// </summary>
            kor,

            /// <summary>
            /// 法语
            /// </summary>
            fra,


            /// <summary>
            /// 德语
            /// </summary>
            de,

            /// <summary>
            /// 自动
            /// </summary>
            auto
        }

        /// <summary>
        /// 语言的键值,存在default里
        /// </summary>
        public emLanguageKey LanguageKey { get; private set; }


        /// <summary>
        /// 文件夹路径
        /// </summary>
        public virtual string Folder { get; set; }


        /// <summary>
        /// 
        /// </summary>
        public virtual string Name
        {
            get
            {
                return new DirectoryInfo(Folder).Name;
            }
        }

        /// <summary>
        /// 获取语言文件
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public LanguageFile this[string name]
        {
            get
            {
                if (name == null)
                {
                    return null;
                }
                return m_Languages.FirstOrDefault(item=>item.Name==name);
            }
        }


        public LanguageFile DefaultLanguage
        {
            get; private set;
        }

        /// <summary>
        /// 获取语言包下的所有语言文件
        /// </summary>
        public List<LanguageFile> LanguageFiles
        {
            get
            {
                return m_Languages;
            }
            set
            {
                m_Languages = value;
            }
        }
        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:2022/4/1 10:29:06
        /// </summary>
        public LanguagePackage(string folderName)
        {
            m_Languages = new List<LanguageFile>();

            this.Folder = folderName;
        }


        public LanguagePackage(string folderPath, string packageName) : this($"{folderPath}\\{packageName}\\")
        {
            Directory.CreateDirectory(this.Folder);
        }
        #endregion

        #region 父类函数重载、接口实现

        protected virtual void Load(string folderName)
        {
            this.Folder = folderName;
            DefaultLanguage = null;
            m_Languages.Clear();
            DirectoryInfo di = new DirectoryInfo(folderName);
            foreach (FileInfo file in di.GetFiles($"*{LanguageFile.FileExtension}"))
            {
                LanguageFile lngFile = new LanguageFile(file.FullName);
                lngFile.Package = this;
                lngFile.ReadStream();
                m_Languages.Add(lngFile);

                if (lngFile.Name.Equals(DefaultLanguageFileName))
                {
                    DefaultLanguage = lngFile;
                }
            }
            if (DefaultLanguage == null)
            {
                DefaultLanguage = GetLangugeFile(DefaultLanguageFileName);
            }

            //读取语言键值
            string key = DefaultLanguage.ReadValue("key", "key", emLanguageKey.zh.ToString());
            if (Enum.TryParse<emLanguageKey>(key, out emLanguageKey result))
            {
                this.LanguageKey = result;
            }
            else
            {
                this.LanguageKey = emLanguageKey.zh;
            }
        }

        public virtual void Load()
        {
            Load(this.Folder);
        }


        public virtual void Write()
        {
            if (!Directory.Exists(Folder))
            {
                Directory.CreateDirectory(Folder);
            }

            foreach (LanguageFile file in this.m_Languages)
            {
                file.WriteStream();
            }
        }

        public virtual void Write(string folderPath)
        {
            if (!Directory.Exists(Folder))
            {
                Directory.CreateDirectory(Folder);
            }

            foreach (LanguageFile file in this.m_Languages)
            {
                file.WriteStream(Path.Combine(folderPath,Path.GetFileName(file.Name+".lng")));
            }
        }



        #region 读取接口

        protected LanguageFile CreateLanguageFile(string fileName)
        {
            LanguageFile rlt = new LanguageFile($"{this.Folder}\\{fileName}{LanguageFile.FileExtension}");
            m_Languages.Add(rlt);
            rlt.Package = this;
            return rlt;
        }

        /// <summary>
        /// 获取语言文件
        /// </summary>
        /// <param name="languageName"></param>
        /// <returns></returns>
        public LanguageFile GetLangugeFile(string languageName)
        {
            if (string.IsNullOrEmpty(languageName))
            {
                if (DefaultLanguage == null)
                {
                    DefaultLanguage = CreateLanguageFile(DefaultLanguageFileName);
                }
                return DefaultLanguage;
            }
            else
            {
                LanguageFile lngFile = this[languageName];
                if (lngFile == null)
                {
                    lngFile = CreateLanguageFile(languageName);
                }
                return lngFile;
            }
        }

        /// <summary>
        /// 读取对应语言文件的键值
        /// </summary>
        /// <param name="languageName"></param>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public virtual string ReadKey(string languageName, string section, string key, string defaultValue)
        {
            LanguageFile lngFile = GetLangugeFile(languageName);

            return lngFile.ReadValue(section, key, defaultValue);
        }


        /// <summary>
        /// 读取对应语言文件的键值
        /// </summary>
        /// <param name="languageName"></param>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual string ReadKey(string languageName, string section, string key)
        {
            LanguageFile lngFile = GetLangugeFile(languageName);
            return lngFile.ReadValue(section, key);
        }


        /// <summary>
        /// 根据枚举类型字串和枚举值读取枚举的语言
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public virtual string ReadEnum(string enumType, string enumValue)
        {
            return ReadKey(EnumLanguageFileName, enumType, enumValue);
        }

        /// <summary>
        /// 根据枚举类型字串和枚举值读取枚举的语言
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public virtual string ReadEnum(string enumType, string enumValue, string defaultValue)
        {
            return ReadKey(EnumLanguageFileName, enumType, enumValue, defaultValue);
        }

        /// <summary>
        /// 和其它语言包合并键值对
        /// </summary>
        /// <param name="otherPackage"></param>
        public virtual void MergeWith(LanguagePackage otherPackage)
        {
            foreach (var otherLangFile in otherPackage.LanguageFiles)
            {
                var languageFile = LanguageFiles.FirstOrDefault(file => file.Name == otherLangFile.Name);
                if (languageFile == null)
                {
                    string path= Path.Combine(this.Folder, Path.GetFileName(otherLangFile.FilePath));
                    var newLangFile = new LanguageFile(path);
                    newLangFile.MergeWith(otherLangFile);
                    this.LanguageFiles.Add(newLangFile);
                    continue;
                }
                languageFile.MergeWith(otherLangFile);
            }
        }


        /// <summary>
        /// 和其它语言包合并键值对
        /// </summary>
        /// <param name="otherPackage"></param>
        public virtual void MergeWith(LanguagePackage otherPackage,out Dictionary<string,List<string>> annotationDict)
        {
            annotationDict = new Dictionary<string, List<string>>();
            foreach (var otherLangFile in otherPackage.LanguageFiles)
            {
                var languageFile = LanguageFiles.FirstOrDefault(file => file.Name == otherLangFile.Name);
                if (languageFile == null)
                {
                    string path = Path.Combine(this.Folder, Path.GetFileName(otherLangFile.FilePath));
                    var newLangFile = new LanguageFile(path);
                    newLangFile.MergeWith(otherLangFile, out List<string> annotations);
                    annotationDict[otherLangFile.Name + ".lng"] = annotations;
                    this.LanguageFiles.Add(newLangFile);
                }
                else
                {
                    languageFile.MergeWith(otherLangFile, out List<string> annotations);
                    annotationDict[otherLangFile.Name + ".lng"] = annotations;
                }
            }
        }

        public virtual void SetPackagePath(string path)
        {
            Folder = path;
            foreach (LanguageFile file in this.LanguageFiles)
            {
                file.FilePath= Path.Combine(path,Path.GetFileName(file.FilePath));
            }
        }


        public void AnnotatePakUnTranslated()
        {
            foreach (LanguageFile file in LanguageFiles)
            {
                foreach(var dict in file.GetIniCache().Values)
                {
                    foreach (LanguageFile.KeyData keyData in dict.Values)
                    {
                        keyData.Comments =new string [] { "待翻译" };
                    }
                }
            }
        }
        #endregion

        #endregion

        #region 函数

        #region private



        #endregion


        #region protected



        #endregion


        #region public



        #endregion

        #endregion
    }
}

